package com.ssoclient.controller;

import cn.hutool.core.util.StrUtil;
import com.ssoclient.config.JwtConfig;
import com.ssoclient.services.IUserService;
import com.ssoclient.utils.CommonCode;
import com.ssoclient.utils.HttpClient;
import com.ssoclient.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 11900
 * @version V1.0
 * @Title: ${FILE_NAME}
 * @Package com.ssoclient.controller
 * @Description: TODO
 * @date 2020/4/27 12:41
 */
@RestController
public class SsoclentController {
    @Value("${sso_server}")
    private String serverHost;
    @Autowired
    private JwtConfig jwtConfig;
    @Autowired
    private IUserService userService;

    @GetMapping("/test")
    public ResultUtil test() {
        return  ResultUtil.build(CommonCode.VALOK);
    }


    @GetMapping("/logout")
    public ResultUtil logout(HttpServletRequest request) throws Exception {
       String token = request.getHeader(jwtConfig.getHeard());
        if (StrUtil.isEmpty(token)) {
            return ResultUtil.build(CommonCode.FAIL);
        }
        HttpClient.get(serverHost + "/inValid",token);
        return ResultUtil.ok();
    }
    @GetMapping("/getUser")
    public ResultUtil getUser(HttpServletRequest request) throws Exception {
       String token = request.getHeader(jwtConfig.getHeard());
        if (StrUtil.isEmpty(token)) {
            return ResultUtil.build(CommonCode.FAIL);
        }
        return ResultUtil.ok(userService.getUserByToken(token));
    }
}
